﻿
using System.Windows.Controls;

namespace Silverstone
{
    public abstract class PageView<TViewModel> : UserControl, IView
        where TViewModel : class, IViewModel
    {
        private TViewModel viewModel;

        public TViewModel ViewModel
        {
            get { return this.viewModel; }
            set 
            {
                if (this.viewModel != null)
                {
                    this.OnViewModelDetached();
                }
                this.viewModel = value;
                value.SetView(this);
                this.DataContext = value;
                this.OnViewModelAttached();
            }
        }

        protected virtual void OnViewModelAttached()
        {
        }

        protected virtual void OnViewModelDetached()
        {
        }

        public virtual void OnLoad()
        {
        }

        public virtual void OnUnload()
        {
        }
    }
}
